//
//  HZL_MusicViewController.m
//  B_Project
//
//  Created by dllo on 15/12/3.
//  Copyright © 2015年 HZL. All rights reserved.
//

#import "HZL_MusicViewController.h"
#import "UIImageView+WebCache.h"
#import "HZL_NetworkingHandle.h"
#import "HZL_MusicTableViewCell.h"
#import "HZL_MusicModel.h"
#import "HZL_PlayViewController.h"
#import "RESideMenu.h"

#define kScreenWidth  [UIScreen mainScreen].bounds.size.width
@interface HZL_MusicViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIImageView *expandZoomImageView; // 背景图
@property (nonatomic, strong) UIView *black;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *score_list;
@property (nonatomic, strong) NSMutableArray *rank_delta_list;
@end

@implementation HZL_MusicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    
    [self creatTableView];
    
    self.navigationController.navigationBar.alpha = 0;
    self.dataArr = [NSMutableArray array];
    self.score_list = [NSMutableArray array];
    self.rank_delta_list = [NSMutableArray array];
    
    [self getData];
}

#pragma mark - 创建tableView
- (void)creatTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithRed:0.13 green:0.13 blue:0.13 alpha:1];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(VIEW_HEIGHT/2, 0, 0, 0);
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:self.tableView];
    
    // 背景图
    self.expandZoomImageView = [[UIImageView alloc]init];
    self.expandZoomImageView.contentMode = UIViewContentModeScaleToFill;
    self.expandZoomImageView.frame = CGRectMake(0, -VIEW_HEIGHT/2, [UIScreen mainScreen].bounds.size.width , VIEW_HEIGHT/2);
    [self.tableView addSubview:self.expandZoomImageView];
    
    self.black = [[UIView alloc] init];
    self.black.frame = CGRectMake(0, -VIEW_HEIGHT/2, [UIScreen mainScreen].bounds.size.width , VIEW_HEIGHT/2);
    self.black.backgroundColor = [UIColor blackColor];
    self.black.alpha = 0.3;
    [self.tableView addSubview:self.black];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setFrame:CGRectMake(20, 20, 50, 50)];
    btn.backgroundColor = [UIColor clearColor];
    [self.black addSubview:btn];
    [btn setImage:[UIImage imageNamed:@"iconfont-liebiao"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(presentLeftMenuViewController:) forControlEvents:UIControlEventTouchUpInside];
    
        
    // 时间
    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, -100, VIEW_WIDTH-15 , 40)];
    self.dateLabel.textColor = [UIColor whiteColor];
    self.dateLabel.font = [UIFont fontWithName:@"KohinoorDevanagari-Medium" size:36];
    [self.tableView addSubview:self.dateLabel];
    
    UIImageView *musicImg = [[UIImageView alloc] initWithFrame:CGRectMake(15, -50, 40, 40)];
    musicImg.image = [UIImage imageNamed:@"iconfont-erji"];
    [self.tableView addSubview:musicImg];
    
    UILabel *musicLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, -50, 120, 40)];
    musicLabel.textColor = [UIColor whiteColor];
    musicLabel.font = [UIFont fontWithName:@"KohinoorDevanagari-Medium" size:34];
    musicLabel.text = @"LISTEN";
    [self.tableView addSubview:musicLabel];
    
    UILabel *ing = [[UILabel alloc] initWithFrame:CGRectMake(170, -50, 60, 40)];
    ing.textColor = [UIColor colorWithRed:28/255.0 green:175/255.0 blue:239/255.0 alpha:1];
    ing.font = [UIFont fontWithName:@"KohinoorDevanagari-Medium" size:34];
    ing.text = @"ING";
    [self.tableView addSubview:ing];

    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(235, -45, 1.5, 30)];
    line.backgroundColor = [UIColor colorWithRed:28/255.0 green:175/255.0 blue:239/255.0 alpha:1];
    [self.tableView addSubview:line];
    
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(240, -50, 40, 40)];
    textLabel.numberOfLines = 0;
    textLabel.font = [UIFont systemFontOfSize:14];
    textLabel.textColor = [UIColor colorWithRed:28/255.0 green:175/255.0 blue:239/255.0 alpha:1];
    textLabel.text = @"最硬排行";
    [self.tableView addSubview:textLabel];
    
}
#pragma mark - 获取数据
- (void)getData
{
    [HZL_NetworkingHandle getUrl:@"http://m.kanjian.com/radio/ing/topic/last" musicCookie:@"52FB49B6-650E-429A-8829-C1A990C6B6F6" result:^(id result) {
        [self.expandZoomImageView sd_setImageWithURL:[NSURL URLWithString:result[@"big_cover"]]];
        self.dateLabel.text = result[@"date"];

        NSMutableArray *temp = result[@"music_list"];
        self.dataArr = [HZL_MusicModel transformWithArray:temp];
        self.score_list = result[@"score_list"];
        self.rank_delta_list = result[@"rank_delta_list"];
        
        [self.tableView reloadData];
    }];
}
#pragma mark - tableView方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
// 自定义分组
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    // 创建一个视图（v_headerView）
    UIView *v_headerView = [[UIView alloc] initWithFrame:CGRectMake(0, -20, VIEW_WIDTH, 30)];
    v_headerView.backgroundColor = [UIColor colorWithRed:0.13 green:0.13 blue:0.13 alpha:1];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 50)];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor colorWithRed:28/255.0 green:175/255.0 blue:239/255.0 alpha:1];
    [v_headerView addSubview:label];
    label.text = @"-- 正在打榜 --";
    return v_headerView;
}
- (CGFloat )tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
#pragma mark - tableView赋值
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HZL_MusicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"music"];
    if (!cell){
        cell = [[HZL_MusicTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"music"];
        }
    cell.backgroundColor = [UIColor colorWithRed:0.13 green:0.13 blue:0.13 alpha:1];
    cell.model = self.dataArr[indexPath.row];
    cell.rank_delta_list = self.rank_delta_list[indexPath.row];
    cell.score_list = self.score_list[indexPath.row];
    cell.index = indexPath.row+1;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}
#pragma mark - tableView点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HZL_PlayViewController *play = [HZL_PlayViewController sharePlayerViewController];
    play.model = self.dataArr[indexPath.row];
    play.indexRow = indexPath.row; // 点击列表row
    play.allMusicArray = self.dataArr; // 总音乐列表
    play.url = nil;
    [self.navigationController pushViewController:play animated:YES];
}
#pragma mark - 滑动时拉大图片方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat yOffset  = scrollView.contentOffset.y ;
    CGFloat xOffset = (yOffset + VIEW_HEIGHT/2)/2;
    if (yOffset < -VIEW_HEIGHT/2) {
        CGRect f = self.expandZoomImageView.frame;
        f.origin.y = yOffset ;
        f.size.height =  -yOffset ;
        f.origin.x = xOffset;
        f.size.width = VIEW_WIDTH + fabs(xOffset)*2;
        self.expandZoomImageView.frame = f;
    }
    
    CGFloat yOffset_black  = scrollView.contentOffset.y ;
    CGFloat xOffset_black = (yOffset_black + VIEW_HEIGHT/2)/2;
    if (yOffset_black < -VIEW_HEIGHT/2) {
        CGRect f = self.black.frame;
        f.origin.y = yOffset_black ;
        f.size.height =  -yOffset_black ;
        f.origin.x = xOffset;
        f.size.width = VIEW_WIDTH + fabs(xOffset_black)*2;
        self.black.frame = f;
    }
    
    
//    //获取当前偏移量
//    //    CGFloat y = scrollView.contentOffset.y;
//    //bar 将要出来时  显隐度一点点改变
//    if (scrollView.contentOffset.y <= 0 && scrollView.contentOffset.y >= -64) {
//        self.navigationController.navigationBar.alpha = (64 + scrollView.contentOffset.y) / 64;
//        self.navigationController.navigationBar.hidden = NO;
//    }
//    else if (scrollView.contentOffset.y > 0)
//    {
//        self.navigationController.navigationBar.alpha = 1.0;
//    }
//    else
//    {
//        self.navigationController.navigationBar.alpha = 0.0;
//    }
}
//#pragma mark - 视图要消失时候 设置bar出现
//- (void)viewWillDisappear:(BOOL)animated {
//    [super viewWillDisappear:YES];
//    self.navigationController.navigationBar.alpha = 1.0;
//    self.navigationController.navigationBar.hidden = NO;
//}
//#pragma mark - 视图要出现时候 隐藏
//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear: animated];
//    self.automaticallyAdjustsScrollViewInsets = NO;
//    self.navigationController.navigationBar.alpha = 0.0;
//    self.navigationController.navigationBar.hidden = YES; //如果不写 从其他页过来 会出现一个navagationBar
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
